﻿#include "ssLogThread.h"
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#else
#include <sys/stat.h>
#endif
#include "../time/ssTime.h"
#include "../ssAssert.h"


namespace StarSeeker
{

ssLogThread::ssLogThread()
{
}

ssLogThread::ssLogThread(u8 type, const char* path)
	:ssLogBase(type, path)
{
}

ssLogThread::~ssLogThread()
{
}

void ssLogThread::OutPut(SS_LOG_TYPE type, const char *pLogString)
{
	ssMsg_s msg;
	OutPut2Msg(type, pLogString, msg);
	PostMsg(msg);
}

void ssLogThread::OutPutEx(SS_LOG_TYPE type, const char *pLogString, ...)
{
	ssMsg_s msg;
	va_list arglist;
	va_start(arglist, pLogString);
	OutPutEx2Msg(type, pLogString, arglist, msg);
	va_end(arglist);
	PostMsg(msg);
}

void ssLogThread::PrintDebug(const char *pLogString)
{
	OutPut(SS_LOG_TYPE::LOG_DEBUG, pLogString);
}

void ssLogThread::PrintDebugEx(const char *pLogString, ...)
{
	ssMsg_s msg;
	va_list arglist;
	va_start(arglist, pLogString);
	OutPutEx2Msg(SS_LOG_TYPE::LOG_DEBUG, pLogString, arglist, msg);
	va_end(arglist);
	PostMsg(msg);
}

void ssLogThread::Print(const char *pLogString)
{
	OutPut(SS_LOG_TYPE::LOG_INFO, pLogString);
}

void ssLogThread::PrintEx(const char *pLogString, ...)
{
	ssMsg_s msg;
	va_list arglist;
	va_start(arglist, pLogString);
	OutPutEx2Msg(SS_LOG_TYPE::LOG_INFO, pLogString, arglist, msg);
	va_end(arglist);
	PostMsg(msg);
}

void ssLogThread::Error(const char *pLogString)
{
	OutPut(SS_LOG_TYPE::LOG_ERROR, pLogString);
}

void ssLogThread::ErrorEx(const char *pLogString, ...)
{
	ssMsg_s msg;
	va_list arglist;
	va_start(arglist, pLogString);
	OutPutEx2Msg(SS_LOG_TYPE::LOG_ERROR, pLogString, arglist, msg);
	va_end(arglist);
	PostMsg(msg);
}

void ssLogThread::PrintFile(const char *pLogString)
{
	OutPut(SS_LOG_TYPE::LOG_FILE_ONLY, pLogString);
}

void ssLogThread::PrintFileEx(const char *pLogString, ...)
{
	ssMsg_s msg;
	va_list arglist;
	va_start(arglist, pLogString);
	OutPutEx2Msg(SS_LOG_TYPE::LOG_FILE_ONLY, pLogString, arglist, msg);
	va_end(arglist);
	PostMsg(msg);
}

void ssLogThread::HandleMessage(ssMsg_s &m)
{
	if (m.msgID == 1110111)
	{
		char* str = (char*)m.param[0];
		if ((m_printFlag&m.subID) && !(m.subID&LOG_FILE_ONLY))
		{
//#ifdef _WIN32
//			::OutputDebugStringA(m_buffer.GetString());
//#endif
			std::cout << str << std::endl;
		}
		if ((m_fileFlag&m.subID) || (m.subID&LOG_FILE_ONLY))
		{
			SaveToFile(str);
		}
		delete [] str;
	}
}

}
